<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class AdminUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $adminuser = $this->route('adminuser');
        if($adminuser){
            // 编辑状态下，此时$adminuser为对象
            $rules = [
                'username' => [
                    'required',
                    // 检查唯一性，并跳过ignore字段
                    Rule::unique('admin_users', 'username')->ignore($adminuser->id)
                ],
                'password' => 'same:password2',
            ];
        }else{
            // 添加状态下，此时$adminuser为null
            $rules = [
                'username' => [
                    'required',
                    Rule::unique('admin_users', 'username'),
                ],
                'password' => 'required|same:password2',
                'password2' => 'required',
            ];
        }
        // dump($adminuser);exit;
        return $rules;
    }

    /**
     * laravel字段名，自定义显示信息
     */
    public function attributes(){
        return [
         'password2'=>'确认密码',   
        ];
    }
}
